package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.api.article.ArticleClientApi;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmUserMapper;
import com.zhentao.model.article.dtos.ArticleDto;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.wemedia.pojo.WmChannel;
import com.zhentao.model.wemedia.pojo.WmNews;
import com.zhentao.model.wemedia.pojo.WmUser;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.utils.common.aliyun.TextModerationPlusUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {//自动审核接口

    @Autowired
    private WmNewsMapper wmNewsMapper;



    @Autowired
    private ArticleClientApi articleClientApi;

    @Autowired
    private WmChannelMapper channelMapper;
    @Autowired
    private WmUserMapper wmUserMapper;

    @Override
    public void autoScanWnMews(Integer id) {
        WmNews wmNews = wmNewsMapper.selectById(id);
        if (wmNews != null) {
            //获取内容
            String title = wmNews.getTitle();

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append(title);
            List<Map> maps = JSON.parseArray(wmNews.getContent(), Map.class);
            for (Map map : maps) {
                //传递过来是个图片
                if (map.get("type").equals("text")) {
                    //把获取到的图片保存到images里面 -list集合
                    stringBuilder.append((String) map.get("value"));
                }
            }



            ResponseResult result = TextModerationPlusUtil.checkContent(stringBuilder.toString());

            if(result.getCode()==600){ //审核通过
                wmNews.setStatus((short)8);
                wmNewsMapper.updateById(wmNews);
                //审核通过之后--》需要同步信息


                ArticleDto articleDto=new ArticleDto();
                BeanUtils.copyProperties(wmNews,articleDto);
                //需不需要补充参数
                //频道名字
                WmChannel wmChannel=channelMapper.selectById(wmNews.getChannelId());
                if(wmChannel!=null) {
                    articleDto.setChannelName(wmChannel.getName());
                }

                //作者id和作者名字  -根据作者的id查询出作者的名字
                WmUser wmUser= wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());
                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setLayout(wmNews.getType());

                //其他的  -自己完成
                //完成同步操作
                ResponseResult result1=articleClientApi.saveArticle(articleDto);
                if(result1.getCode()==200){  //同步成功
                    //进行关联
                    wmNews.setArticleId((Long)result1.getData());
                    //是否发布
                    Date date=wmNews.getPublishTime();
                    if(!date.after(new Date())){
                        //发布  状态改成9
                        wmNews.setStatus((short)9);
                    }
                    wmNewsMapper.updateById(wmNews);
                }
            }else if(result.getCode()==602){ //转人工
                wmNews.setStatus((short)3);
                wmNewsMapper.updateById(wmNews);

            }else{  //审核拒绝
                wmNews.setStatus((short)2);
                wmNews.setReason(result.getErrorMessage());
                wmNewsMapper.updateById(wmNews);
            }
        }
    }
}